import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.util.TypeUtils;

import java.lang.reflect.Array;
import java.util.*;

/**
 * @desc: TODO
 * @author: zhangjiaqi
 * @time: 2020/11/27 17:08
 */
public class SerializaUtils {
    public static Object toJSON(Object javaObject) {
        if (javaObject == null) {
            return null;
        } else if (javaObject instanceof JSON) {
            return javaObject;
        } else {
            JSONObject json;
            int len;
            if (javaObject instanceof Map) {
                Map<Object, Object> map = (Map) javaObject;
                len = map.size();
                Object innerMap;
                if (map instanceof LinkedHashMap) {
                    innerMap = new LinkedHashMap(len);
                } else if (map instanceof TreeMap) {
                    innerMap = new TreeMap();
                } else {
                    innerMap = new HashMap(len);
                }
                json = new JSONObject((Map) innerMap);
                Iterator var24 = map.entrySet().iterator();
                while (var24.hasNext()) {
                    Map.Entry<Object, Object> entry = (Map.Entry) var24.next();
                    Object key = entry.getKey();
                    String jsonKey = TypeUtils.castToString(key);
                    Object jsonValue = toJSON(entry.getValue());
                    json.put(jsonKey, jsonValue);
                }
                return json;
            } else {
                if (javaObject instanceof Collection) {
                    Collection<Object> collection = (Collection) javaObject;
                    JSONArray array = new JSONArray(collection.size());
                    Iterator var19 = collection.iterator();
                    while (var19.hasNext()) {
                        Object item = var19.next();
                        item = toJSON(item);
                        array.add(item);
                    }
                    return array;
                } else if (javaObject instanceof ReSerializeModel) {
                    return ((ReSerializeModel) javaObject).toJson();
                } else {
                    Class<?> clazz = javaObject.getClass();
                    if (clazz.isEnum()) {
                        return ((Enum) javaObject).name();
                    } else if (clazz.isArray()) {
                        len = Array.getLength(javaObject);
                        JSONArray array = new JSONArray(len);
                        for (int i = 0; i < len; ++i) {
                            Object item = Array.get(javaObject, i);
                            Object jsonValue = toJSON(item);
                            array.add(jsonValue);
                        }
                        return array;
                    } else if (ParserConfig.isPrimitive2(clazz)) {
                        return javaObject;
                    } else {
                        // 未继承ISerializa的未知对象 不进行转换
                        return null;
                    }
                }
            }
        }
    }
}
